<template>
  <div v-loading="loading" element-loading-text="加载中。。。">
    <el-card style="margin-bottom:10px">
      <el-form ref='formInline' :model='formInline' size='mini' label-position="top">
        <el-row :gutter="10">
          <el-col :lg="4" :md="6">
            <el-form-item label='应用编号' prop='AppNum'>
              <el-select v-model="formInline.AppNum" placeholder="请选择应用编号">
                <el-option v-for="item in appQueryList" :key="item.devState" :label="item.AppNum+'-'+item.AppName" :value="item.AppNum" :disabled="item.disabled">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label='交易组编号' prop='TranGroupNum'>
              <el-input v-model='formInline.TranGroupNum' placeholder="请输入交易组编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label='交易组名称' prop='TranGroupName'>
              <el-input v-model='formInline.TranGroupName' placeholder="请输入交易组名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop">
            <el-button type='primary' size='mini' icon='el-icon-search' @click="tradingGroupQuery">查询</el-button>
          </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop">
            <el-button type='warning' size='mini' icon='el-icon-refresh' @click="resetForm('formInline')">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card style="margin-top:10px;">
      <el-row>
        <el-col :lg="2" :md="3">
          <el-button type='primary' size='mini' icon='el-icon-plus' v-if="btnPermission('Button_DevBrand_Add')" @click='tradingGroupAdd()'>新增</el-button>
        </el-col>
        <el-col :lg="2" :md="3">
          <el-button type='primary' size='mini' icon='el-icon-edit' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Edit')" @click='tradingGroupUpdate()'>修改</el-button>
        </el-col>
        <el-col :lg='2' :md='3'>
          <el-button type='danger' size='mini' icon='el-icon-delete' class='b-button' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Delete')" @click='delDialogShow'>删除</el-button>
        </el-col>
        <el-col :lg='2' :md='3'>
          <el-button type='primary' size='mini' icon='el-icon-edit' class='b-button' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Delete')" @click='tranAssign'>交易分配</el-button>
        </el-col>
      </el-row>
      <!-- 列表 -->
      <el-table ref="dataTypeInfo" :data='tranGroupInfoList' border min-height="400" class="tableMarginTop" header-row-class-name="tableHeaderClass" highlight-current-row @selection-change="handleCurrentChangeTable">
        <el-table-column type="selection" width="40px"></el-table-column>
        <el-table-column prop='appnum' label='应用编号'></el-table-column>
        <el-table-column prop='trangroupnum' label='交易组编号'></el-table-column>
        <el-table-column prop='trangroupname' label='交易组名称'></el-table-column>
        <el-table-column prop='trangroupstatus' :formatter="dataFormat" label='交易组状态' min-width='160'></el-table-column>
      </el-table>
      <div style="text-align:right;margin-top:20px">
        <el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="10" layout="total, prev, pager, next, jumper" :total="pageSize">
        </el-pagination>
      </div>
    </el-card>
    <!-- 弹窗 -->
    <keep-alive>
      <component :is='dialogList.createTradingGroup' :createBrand='dialogOptions' @refreshBrand="tradingGroupQuery"></component>
    </keep-alive>
    <keep-alive>
      <component :is='dialogList.updateTradingGroup' :option='UpdateDialogOptions' @refreshBrand="tradingGroupQuery"></component>
    </keep-alive>
    <keep-alive>
      <component :is='dialogList.tranAssign' :option='tranAssignDialogOptions' @refreshBrand="tradingGroupQuery"></component>
    </keep-alive>
  </div>
</template>

<script>
import toolBox from "@/common/utils/toolBox";
import request from "@/common/utils/request";
import selectTree from "@/components/selectTree3/";
import getGlobalParams from "@/common/utils/getGlobalParams";
import tranAssign from "./dialog/tranAssign";
import createTradingGroup from "./dialog/createTradingGroup";
import updateTradingGroup from "./dialog/updateTradingGroup";
import appQuery from "@/message/shared/appquery"; // 应用编号查询
import branchList from "@/message/shared/branch-list";
import tranGroup from "@/message/branchManage/tranGroupManage/tranGroup";

export default {
  components: { selectTree },
  data() {
    return {
      dialogList: { createTradingGroup, updateTradingGroup, tranAssign },
      dialogOptions: { isShow: false },
      UpdateDialogOptions: { isShow: false },
      tranAssignDialogOptions: { isShow: false },
      formInline: {
        AppNum: "",
        TranGroupNum: undefined,
        TranGroupName: undefined
      },
      appQueryList: [], // 设备应用下拉框
      tranGroupInfoList: [],
      loading: false,
      currentPage: 1,
      pageSize: undefined,
      checkedUser: false,
      currentRow: [],
      tranGroupStatus: {}
    };
  },
  created() {
    this.tranGroupStatus = getGlobalParams.get("postStatus");
    this.tradingGroupQuery();
    this.appQueryInit();
  },
  methods: {
    // 设备型号下拉框初始化查询
    appQueryInit() {
      let resBody = new appQuery();
      resBody.data.UserNum = this.$store.getters.UserNum;
      request(resBody)
        .then(response => {
          this.appQueryList = response.RspInfo.Result;
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 删除
    delTranGroup() {
      let resBody = new tranGroup();
      this.loading = true;
      resBody.data.AppNum = this.currentRow.appnum;
      resBody.data.TranGroupNum = this.currentRow.trangroupnum;
      resBody.data.OperType = "D"; // 操作类型
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.loading = false;
            this.$message({
              type: "success",
              message: "删除成功"
            });
            this.tradingGroupQuery();
          } else {
            this.loading = false;
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          this.loading = false;
          this.$message({
            type: "info",
            message: error
          });
        });
    },

    //删除厂商弹窗
    delDialogShow() {
      this.$confirm("此操作将永久删除该交易组, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false
      })
        .then(() => {
          this.delTranGroup();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleCurrentChangeTable(val) {
      //保存当前选中数据并判断按钮是否可用
      this.currentRow = val[0];
      this.UpdateDialogOptions.currentRow = this.currentRow;
      this.tranAssignDialogOptions.currentRow = this.currentRow;
      if (val.length == 1) {
        this.checkedUser = true;
      } else if (val.length > 1) {
        this.checkedUser = false;
      } else {
        this.checkedUser = false;
      }
    },
    dataFormat(row, column) {
      return this.tranGroupStatus[row[column.property]];
    },
    //打开添加弹窗
    tradingGroupAdd() {
      this.dialogOptions.isShow = true;
    },

    //打开修改弹窗
    tradingGroupUpdate() {
      this.UpdateDialogOptions.isShow = true;
    },

    //打开交易分配弹窗
    tranAssign() {
      this.tranAssignDialogOptions.isShow = true;
    },

    //清除表单数据
    resetForm(item) {
      this.$refs["" + item].resetFields();
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.tradingGroupQuery();
    },

    /**
     * 查询表格数据
     */
    tradingGroupQuery() {
      this.loading = true;
      let resBody = new tranGroup();
      if (this.formInline.AppNum) {
        resBody.data.AppNum = this.formInline.AppNum;
      } else {
        resBody.data.UserNum = this.$store.getters.UserNum;
      }
      resBody.data.TranGroupName = this.formInline.TranGroupName;
      resBody.data.TranGroupNum = this.formInline.TranGroupNum;
      resBody.data.LegalPersonNum = this.$store.getters.LegalPersonNum;
      resBody.data.OperType = "Q"; // 操作类型
      // 查询公共参数
      resBody.data.length = getGlobalParams.get("PageSize");
      resBody.data.draw = this.currentPage;
      resBody.data.start =
        (this.currentPage - 1) * getGlobalParams.get("PageSize");
      request(resBody)
        .then(response => {
          this.loading = false;
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.tranGroupInfoList = response.RspInfo.pageList || [];
            this.pageSize = response.RspInfo.recordsTotal;
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          this.loading = false;
          //   this.logger.log("fail", error);
        });
    }
  }
};
</script>


<style scoped lang="scss">
</style>
